package com.knowif.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.knowif.dao.TUserMapper;
import com.knowif.dao.TutorInfoMapper;
import com.knowif.dao.UserCollectMapper;
import com.knowif.dao.UserCommentMapper;
import com.knowif.enumInfo.InfoStatusEnum;
import com.knowif.pojo.TutorInfo;
import com.knowif.pojo.TutorInfoExample;
import com.knowif.pojo.UserCollect;
import com.knowif.pojo.UserCollectExample;
import com.knowif.system.model.RestBean;

@Service
public class UserCollect_service {

	@Autowired
	public TUserMapper userMapper;

	@Autowired
	public UserCommentMapper userCommentMapper;

	@Autowired
	public UserCollectMapper userCollectMapper;

	@Autowired
	public TutorInfoMapper tutorInfoMapper;

	@Autowired
	public Tutor_service tutor_service;

	/**
	 * @author: xingyao
	 * @Description: 用户收藏实现
	 * @param userId
	 *            收藏者id
	 * @param beUserId
	 *            被收藏者id
	 * @return
	 */
	@Transactional
	public RestBean<?> addCollect(Integer userId, Integer beUserId) {
		if (userId == null || userId == 0) {
			return RestBean.buildError("请输入userId不能为空");
		}
		if (beUserId == null || beUserId == 0) {
			return RestBean.buildError("请输入被收藏者id不能为空");
		}
		// 先进行查找，看是否有这个收藏，如果有则直接返回成功
		UserCollectExample userCollectExample = new UserCollectExample();
		userCollectExample.createCriteria().andUserIdEqualTo(userId).andBeCollectedUserIdEqualTo(beUserId);
		List<UserCollect> collects = userCollectMapper.selectByExample(userCollectExample);
		if (collects != null && !collects.isEmpty()) {
			return deleteCollects(UserCollect.getUserCollectIds(collects));
		}

		UserCollect userCollect = new UserCollect();
		userCollect.setUserId(userId);
		userCollect.setBeCollectedUserId(beUserId);
		userCollectMapper.insert(userCollect);

		TutorInfoExample tutorInfoExample = new TutorInfoExample();
		tutorInfoExample.createCriteria().andUserIdEqualTo(beUserId);
		TutorInfo tutorInfo = new TutorInfo();
		Integer collectedNum = tutorInfo.getBeCollectedNum();
		if(collectedNum == null){
			collectedNum = 0;
		}
		tutorInfo.setBeCollectedNum((collectedNum + 1));
		int updateNum = tutorInfoMapper.updateByExampleSelective(tutorInfo, tutorInfoExample);
		if (updateNum != 1) {
			throw new RuntimeException("收藏导师失败");
		}
		return RestBean.buildSuccess(InfoStatusEnum.ISBECOLLECT.getStatus());
	}

	/**
	 * @author: xingyao
	 * @Description: 查看用户收藏导师信息
	 * @param searchCondition
	 * @return
	 */
	public RestBean<?> selectCollect(UserCollectExample userCollectExample) {
		if (userCollectExample == null) {
			return RestBean.buildError("请输入查询条件");
		}
		List<UserCollect> userCollects = userCollectMapper.selectByExample(userCollectExample);
		if (userCollects == null) {
			return RestBean.buildSuccess("没有收藏导师");
		}
		List<Integer> beUserIds = UserCollect.getUserIds(userCollects);
		List<TutorInfo> tutorInfos = tutor_service.selectByUserIds(beUserIds);
		return RestBean.buildSuccess(tutorInfos);
	}
	
	
	/**
	 * @author: xingyao 
	 * @Description:  判断该导师是否被该用户收藏
	 * @param userId
	 * @param tutorId
	 * @return
	 */
	public boolean isBeCollect(Integer userId,Integer tutorId){
		if(userId == null || tutorId == null){
			return false;
		}
		UserCollectExample userCollectExample = new UserCollectExample();
		userCollectExample.createCriteria().andUserIdEqualTo(userId).andBeCollectedUserIdEqualTo(tutorId);
		List<UserCollect> userCollects = userCollectMapper.selectByExample(userCollectExample);
		if(userCollects == null || userCollects.isEmpty()){
			return false;
		}
		return true;
	}

	/**
	 * @author: xingyao
	 * @Description: 删除用户收藏导师记录
	 * @param userCollectId
	 * @return
	 */
	@Transactional
	public RestBean<?> deleteCollect(Integer userCollectId) {
		if (userCollectId == null) {
			return RestBean.buildError("请选择取消收藏导师的id");
		}
		UserCollectExample userCollectExample = new UserCollectExample();
		userCollectExample.createCriteria().andIdEqualTo(userCollectId);
		List<UserCollect> userCollects = userCollectMapper.selectByExample(userCollectExample);
		if (userCollects == null || userCollects.isEmpty()) {
			return RestBean.buildError("你还没有收藏过这名导师");
		}
		List<Integer> userIds = UserCollect.getUserIds(userCollects);
		tutorInfoMapper.updateCollectNumByUserIds(userIds);
		userCollectMapper.deleteByExample(userCollectExample);

		return RestBean.buildSuccess("取消收藏导师成功");
	}

	/**
	 * @author: xingyao
	 * @Description: 删除用户收藏导师记录
	 * @param userCollectId
	 * @return
	 */
	@Transactional
	public RestBean<?> deleteCollects(List<Integer> userCollectIds) {
		if (userCollectIds == null || userCollectIds.isEmpty()) {
			return RestBean.buildError("请选择要删除导师的信息");
		}
		UserCollectExample userCollectExample = new UserCollectExample();
		userCollectExample.createCriteria().andIdIn(userCollectIds);
		List<UserCollect> userCollects = userCollectMapper.selectByExample(userCollectExample);
		if (userCollects == null || userCollects.isEmpty()) {
			return RestBean.buildError("你还没有收藏过这名导师");
		}
		List<Integer> userIds =UserCollect.getUserIds(userCollects);
		tutorInfoMapper.updateCollectNumByUserIds(userIds);
		// 在删除收藏记录
		userCollectMapper.deleteByExample(userCollectExample);
		return RestBean.buildSuccess(InfoStatusEnum.ISNOTBECOLLECT.getStatus());
	}
	
	
	//通过userId删除收藏用户
	public boolean deleteCollectByUserId(Integer userId){
		if(userId == null){
			return false;
		}
		UserCollectExample userCollectExample = new UserCollectExample();
		userCollectExample.createCriteria().andUserIdEqualTo(userId);
		List<UserCollect> userCollects = userCollectMapper.selectByExample(userCollectExample);
		if(userCollects == null || userCollects.isEmpty()){
			return true;
		}
		List<Integer> userCollectIds = UserCollect.getUserIds(userCollects);
		deleteCollects(userCollectIds);
		return true;
	}

}













